package com.sx.micowell.framework.arouth

import android.content.Context
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.mvvm.until.LogUtils
import com.miloyu.modelnet.response.*
import com.sx.micowell.framework.app.IntentKey
import com.sx.micowell.framework.entity.AccountBean
import com.sx.micowell.module.collect.MineCollectFragment
import com.sx.micowell.module.countdown.FragCourseCountDownFragment
import com.sx.micowell.module.countdown.FragPlayCountDownFragment
import com.sx.micowell.module.course.ChildCourseListFragment
import com.sx.micowell.module.course.CourseTrainEndFragment
import com.sx.micowell.module.course.video.CourseVideoFragment
import com.sx.micowell.module.course.RecommendCourseFragment
import com.sx.micowell.module.course.video.CourseVideoPlayFragment
import com.sx.micowell.module.help.HelpCenterListFragment
import com.sx.micowell.module.help.HelpDetailsFragment
import com.sx.micowell.module.homePage.*
import com.sx.micowell.module.homePage.home.HomeAllFragment
import com.sx.micowell.module.homePage.home.HomeLastMonthFragment
import com.sx.micowell.module.homePage.home.HomeLastWeekFragment
import com.sx.micowell.module.invite.InviteFriendFragment
import com.sx.micowell.module.learned.LearnedCourseFragment
import com.sx.micowell.module.login.WxQRCodeLoginFragment
import com.sx.micowell.module.login.agreement.UserAgreementFragment
import com.sx.micowell.module.member.*
import com.sx.micowell.module.more.MoreChildCourseListFragment
import com.sx.micowell.module.more.MoreCourseFragment
import com.sx.micowell.module.personal.PersonalInfoFragment
import com.sx.micowell.module.setting.SettingFragment
import com.sx.micowell.module.smartDevice.*
import com.sx.micowell.module.sport.end.SportEndFragment
import com.sx.micowell.module.switchlogin.SwitchLoginFragment
import com.sx.micowell.module.train.TrainStepFragment
import com.sx.micowell.module.train.plan.TrainStepPlanFragment
import com.sx.micowell.module.train.run.scene.TrainRunSceneFragment
import com.sx.micowell.module.train.run.time.TrainRunTimeFragment
import com.sx.micowell.module.train.setting.TrainStepSettingFragment

/**
 * @Description
 * @Author 梁浩文
 * @Date 2021/6/25 14:08
 */
class RouteManager {
    companion object {
        private var instance: RouteManager? = null

        @Synchronized
        fun getInstance(): RouteManager {
            if (instance == null) {
                instance = RouteManager()
            }
            return instance!!
        }
    }

    fun intentToMain() =ARouter.getInstance().build(RoutePath.MAIN).navigation()

//    fun getHomeFragment(context: Context): HomeFragment =
//        ARouter.getInstance().build(RoutePath.HOME).navigation(context) as HomeFragment

    fun getHomeFragment(context: Context): Home2Fragment =
        ARouter.getInstance().build(RoutePath.HOME).navigation(context) as Home2Fragment

    fun getHomeAllFragment(): HomeAllFragment =
        ARouter.getInstance().build(RoutePath.HOME_ALL).navigation() as HomeAllFragment

    fun getHomeLastMonthFragment(): HomeLastMonthFragment =
        ARouter.getInstance().build(RoutePath.HOME_LAST_MONTH).navigation() as HomeLastMonthFragment

    fun getHomeLastWeekFragment(): HomeLastWeekFragment =
        ARouter.getInstance().build(RoutePath.HOME_LAST_WEEK).navigation() as HomeLastWeekFragment

    fun getPlanFragment(context: Context): PlanFragment =
        ARouter.getInstance().build(RoutePath.Plan).navigation(context) as PlanFragment

    fun getTrainFragment(context: Context): TrainFragment =
        ARouter.getInstance().build(RoutePath.Train).navigation(context) as TrainFragment

    fun getMineFragment(context: Context): MineFragment =
        ARouter.getInstance().build(RoutePath.Mine).navigation(context) as MineFragment

    fun intentToHeight(type: String = "", date: String = "") {
        var bundle = Bundle()
        bundle.putString(IntentKey.TYPE, type)
        bundle.putString(IntentKey.DATA, date)
        ARouter.getInstance().build(RoutePath.WRITE_HEIGHT).with(bundle).navigation()
    }

    fun intentToWeight(type: String = "", date: String = "", height: String = "") {
        val bundle = Bundle()
        bundle.putString(IntentKey.TYPE, type)
        bundle.putString(IntentKey.DATA, date)
        bundle.putString(IntentKey.KEY, height)
        ARouter.getInstance().build(RoutePath.WRITE_WEIGHT).with(bundle).navigation()
    }

    fun intentToSexAge() = ARouter.getInstance().build(RoutePath.WRITE_SEX_AGE).navigation()

    fun intentToTarget(type: String = "", date: String = "", height: String = "",
                       initWeight: String = "", targetWeight: String) {
        val bundle = Bundle()
        bundle.putString(IntentKey.TYPE, type)
        bundle.putString(IntentKey.DATA, date)
        bundle.putString(IntentKey.KEY, height)
        bundle.putString(IntentKey.ID, initWeight)
        bundle.putString(IntentKey.MSG, targetWeight)
        ARouter.getInstance().build(RoutePath.WRITE_TARGET).with(bundle).navigation()
    }

    fun getUserAgreementFragment(userAgreement: UserAgreementVO?) : UserAgreementFragment {
        LogUtils.d("getUserAgreementFragment: ${userAgreement?.userAgreement}")
        val bundle = Bundle()
        bundle.putSerializable(IntentKey.DATA, userAgreement)
        return ARouter.getInstance().build(RoutePath.USER_AGREEMENT).with(bundle).navigation() as UserAgreementFragment
    }

    fun getMemberAgreementFragment(userAgreement: VipNotesVO?) : MemberAgreementFragment {
        LogUtils.d("getUserAgreementFragment: ${userAgreement?.vipNotes}")
        val bundle = Bundle()
        bundle.putSerializable(IntentKey.DATA, userAgreement)
        return ARouter.getInstance().build(RoutePath.MEMBER_AGREEMENT).with(bundle).navigation() as MemberAgreementFragment
    }

    fun getOpenMemberFragment(userAgreement: String?) : OpenMemberFragment {
        LogUtils.d("getUserAgreementFragment: $userAgreement")
        val bundle = Bundle()
        bundle.putString(IntentKey.DATA, userAgreement)

        return ARouter.getInstance().build(RoutePath.OPEN_MEMBER_DATA).with(bundle).navigation() as OpenMemberFragment
    }


    fun getWxQRCodeFragment() : WxQRCodeLoginFragment {
        return ARouter.getInstance().build(RoutePath.WX_QR_CODE_LOGIN).navigation() as WxQRCodeLoginFragment
    }

    fun intentLogin(accountBean: AccountBean? = null) = ARouter.getInstance().build(RoutePath.LOGIN).with(Bundle().also {
        it.putSerializable(IntentKey.DATA, accountBean)
    }).navigation()


    fun intentBindPhone(tokenVO: TokenVO? = null) =
        ARouter.getInstance().build(RoutePath.BIND_PHONE).with(
            Bundle().also { it.putSerializable(IntentKey.DATA, tokenVO)
        }).navigation()

    fun intentRegisterToForgetPwd(type: Int = 0, phone: String = "") {
        val bundle = Bundle()
        bundle.putInt(IntentKey.TYPE, type)
        bundle.putString(IntentKey.DATA, phone)
        ARouter.getInstance().build(RoutePath.REGISTER_TO_FORGET_PWD).with(bundle).navigation()
    }

    fun intentVerifyCode(data: String, type: Int = 0, openId: String = "") {
        val bundle = Bundle()
        bundle.putString(IntentKey.DATA, data)
        bundle.putInt(IntentKey.TYPE, type)
        bundle.putString(IntentKey.ID, openId)
        ARouter.getInstance().build(RoutePath.VERIFY_CODE).with(bundle).navigation()
    }

    fun intentSetupPwd(phone : String, verifyCode: String, type: Int) {
        val bundle = Bundle()
        bundle.putString(IntentKey.DATA, phone)
        bundle.putString(IntentKey.ID, verifyCode)
        bundle.putInt(IntentKey.TYPE, type)
        ARouter.getInstance().build(RoutePath.SETUP_PWD).with(bundle).navigation()
    }

    fun intentMineNews() = ARouter.getInstance().build(RoutePath.MINE_NEWS).navigation()

    fun intentNewsDetails(data: SysNotice?) {
        val bundle = Bundle()
        bundle.putSerializable(IntentKey.DATA, data)
        ARouter.getInstance().build(RoutePath.NEWS_DETAILS).with(bundle).navigation()
    }

    fun intentSelectDate() = ARouter.getInstance().build(RoutePath.SELECT_DATE).navigation()

    fun intentSelectTrainType(startTime: String?, endTime: String? ) {
       val bundle = Bundle()
        bundle.putString(IntentKey.DATA, startTime)
        bundle.putString(IntentKey.ID, endTime)
        ARouter.getInstance().build(RoutePath.SELECT_TRAIN_TYPE).with(bundle).navigation()
    }

    fun getMemberFragment(userInfo: UserInfoVO) : MemberFragment =
        ARouter.getInstance().build(RoutePath.OPEN_MEMBER).with(Bundle().also {
            it.putSerializable(IntentKey.DATA, userInfo)
        }).navigation() as MemberFragment


    fun getPaymentListFragment(memberSetMeal: MemberSetMealVO? ) : PaymentListFragment {
        val bundle = Bundle()
        bundle.putSerializable(IntentKey.DATA, memberSetMeal)
        return ARouter.getInstance().build(RoutePath.PAYMENT_LIST)
            .with(bundle).navigation() as PaymentListFragment
    }

    fun getPayQrCodeFragment(data:String?) : PayQrCodeFragment {
        return ARouter.getInstance().build(RoutePath.PAY_QR_CODE).with(
            Bundle().also { it.putString(IntentKey.DATA, data) }
        ).navigation() as PayQrCodeFragment
    }


    fun getMineCollectFragment() : MineCollectFragment =
        ARouter.getInstance().build(RoutePath.MINE_COLLECT).navigation() as MineCollectFragment

    fun getLearnedCourseFragment() : LearnedCourseFragment =
        ARouter.getInstance().build(RoutePath.LEARNED_COURSE).navigation() as LearnedCourseFragment

    fun getSettingFragment() : SettingFragment =
        ARouter.getInstance().build(RoutePath.SETTING).navigation() as SettingFragment

    fun getInviteFriendFragment(): InviteFriendFragment =
        ARouter.getInstance().build(RoutePath.INVITE_FRIENDS).navigation() as InviteFriendFragment

    fun getPersonalInfoFragment() : PersonalInfoFragment =
        ARouter.getInstance().build(RoutePath.PERSONAL_INFO).navigation() as PersonalInfoFragment

    fun getSwitchLoginFragment() : SwitchLoginFragment =
        ARouter.getInstance().build(RoutePath.SWITCH_LOGIN).navigation() as SwitchLoginFragment

    fun getHelpCenterFragment() : HelpCenterListFragment =
        ARouter.getInstance().build(RoutePath.HELP_CENTER_LIST).navigation() as HelpCenterListFragment

    fun getHelpDetailsFragment(data: HelpInfoVO) : HelpDetailsFragment {
        val bundle = Bundle()
        bundle.putSerializable(IntentKey.DATA, data)
        return ARouter.getInstance().build(RoutePath.HELP_DETAILS)
            .with(bundle).navigation() as HelpDetailsFragment
    }

    fun getMoreCourseFragment() : MoreCourseFragment =
        ARouter.getInstance().build(RoutePath.MORE_COURSE).navigation() as MoreCourseFragment

    fun getRecommendCourseFragment() : RecommendCourseFragment =
        ARouter.getInstance().build(RoutePath.RECOMMEND_COURSE).navigation() as RecommendCourseFragment

    fun getChildCourseListFragment(curriculumType:String): ChildCourseListFragment =
        ARouter.getInstance().build(RoutePath.RECOMMEND_COURSE_LIST).with(Bundle().also { it.putString(IntentKey.TYPE,curriculumType) }).navigation() as ChildCourseListFragment

    fun getMoreChildCourseListFragment(curriculumType:String): MoreChildCourseListFragment =
        ARouter.getInstance().build(RoutePath.MORE_RECOMMEND_COURSE_LIST).with(
            Bundle().also { it.putString(IntentKey.TYPE,curriculumType) })
            .navigation() as MoreChildCourseListFragment

    fun getCourseVideoFragment(id: String): CourseVideoFragment =
        ARouter.getInstance().build(RoutePath.COURSE_VIDEO).with(
            Bundle().also { it.putString(IntentKey.KEY, id) }
        ).navigation() as CourseVideoFragment

    fun getVideoPlayFragment(courseVideoData: CourseVideoVO): CourseVideoPlayFragment =
        ARouter.getInstance().build(RoutePath.COURSE_VIDEO_PLAY).with(
            Bundle().also { it.putSerializable(IntentKey.DATA, courseVideoData) }
        ).navigation() as CourseVideoPlayFragment

    fun getCourseTrainEndFragment(trainTime: Long, courseVideoData: CourseVideoVO): CourseTrainEndFragment =
        ARouter.getInstance().build(RoutePath.COURSE_TRAIN_END).with(
            Bundle().also {
                it.putLong(IntentKey.KEY, trainTime)
                it.putSerializable(IntentKey.DATA, courseVideoData)
            }
        ).navigation() as CourseTrainEndFragment

    fun getTrainStepFragment(type: Int): TrainStepFragment =
        ARouter.getInstance().build(RoutePath.Train_STEP).with(Bundle().also { it.putInt(IntentKey.TYPE,type) }).navigation() as TrainStepFragment

    fun getTrainStepTrainFragment(curriculumType :String ?=null): TrainStepPlanFragment =
        ARouter.getInstance().build(RoutePath.TRAIN_STEP_PLAN).with(Bundle().also { curriculumType?.run {it.putString(IntentKey.ID,this)   }}).navigation() as TrainStepPlanFragment

    fun getTrainStepSettingFragment(ids :String): TrainStepSettingFragment =
        ARouter.getInstance().build(RoutePath.TRAIN_STEP_SETTING).with(Bundle().also { it.putString(IntentKey.ID,ids) }).navigation() as TrainStepSettingFragment

    fun getFragPlayCountDownFragment(type: Int,data:String): FragPlayCountDownFragment =
        ARouter.getInstance().build(RoutePath.PLAY_COUNT_DOWN).with(Bundle().also { it.putInt(IntentKey.TYPE,type)
        it.putString(IntentKey.DATA,data)}).navigation() as FragPlayCountDownFragment


    fun getFragCourseCountDownFragment(courseVideoData: CourseVideoVO): FragCourseCountDownFragment =
        ARouter.getInstance().build(RoutePath.COURSE_COUNT_DOWN).with(
            Bundle().also { it.putSerializable(IntentKey.DATA, courseVideoData) }
        ).navigation() as FragCourseCountDownFragment

    fun intentSportActivity(data: String?=""){
        ARouter.getInstance().build(RoutePath.SPORT_START).with(Bundle().also { it.putString(IntentKey.DATA,data) }).navigation()
    }

    fun getTrainRunTimeFragment(): TrainRunTimeFragment =
        ARouter.getInstance().build(RoutePath.SPORT_TIME).navigation() as TrainRunTimeFragment

    fun getTrainRunSceneFragment(ids :String): TrainRunSceneFragment =
        ARouter.getInstance().build(RoutePath.RUN_SCENE).with(Bundle().also { it.putString(IntentKey.ID,ids) }).navigation() as TrainRunSceneFragment

    fun getSportEndFragment(CALORIE :String,TIME :String): SportEndFragment =
        ARouter.getInstance().build(RoutePath.SPORT_END).with(Bundle().also { it.putString(IntentKey.CALORIES,CALORIE)
            it.putString(IntentKey.TIME,TIME)}).navigation() as SportEndFragment



    /**
     * 智能设备
     */
    fun intentDeviceLogin() {
        ARouter.getInstance().build(RoutePath.SMART_DEVICE_LOGIN).navigation()
    }

    fun intentDeviceRegister(type: Int) {
        ARouter.getInstance().build(RoutePath.SMART_DEVICE_REGISTER).with(
            Bundle().also { it.putInt(IntentKey.TYPE, type) }
        ).navigation()
    }

    fun getDeviceListFragment() : SmartDeviceBoundListFragment =
        ARouter.getInstance().build(RoutePath.SMART_DEVICE_LIST).navigation() as SmartDeviceBoundListFragment

    fun getDeviceBindListFragment() : DeviceBindListFragment =
        ARouter.getInstance().build(RoutePath.SMART_DEVICE_BIND_LIST).navigation() as DeviceBindListFragment

    fun getSelectAddDeviceFragment() : SelectAddDeviceFragment =
        ARouter.getInstance().build(RoutePath.SMART_SELECT_ADD_DEVICE).navigation() as SelectAddDeviceFragment

    fun getSearchDeviceFragment() : SearchDeviceFragment =
        ARouter.getInstance().build(RoutePath.SMART_SEARCH_DEVICE).navigation() as SearchDeviceFragment

    fun getDeviceDataFragment(data: String, id: String) : SmartDeviceDataFragment =
        ARouter.getInstance().build(RoutePath.SMART_DEVICE_DATA).with(
            Bundle().also {
                it.putString(IntentKey.DATA, data)
                it.putString(IntentKey.ID, id)
            }
        ).navigation() as SmartDeviceDataFragment

    fun intentMkVideoVideo(mp4 :String,url :String){
        ARouter.getInstance().build(RoutePath.MK_VIDEO_VIEW).with( Bundle().also {
            it.putString(IntentKey.MP4,mp4)
            it.putString(IntentKey.URL,url)
        }).navigation()
    }
}